<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

class Category{

	var $db;
	
	function __construct() {
		global $db;
		$this->db = $db;
	}
	
	function get_category( $count = false )
	{
		$category = cache_read( 'article_category' );
		if( $count )
		{
			foreach( $category as $key => $value )
			{
				$category[$key]['article_no'] = $this->db->count( DB_PRE.'article', array( 'cid' => $value['id'], 'draft' => 0 ) );
			}
		}
		return $category;
	}
	
	
	/**
	 * 获取模板
	 *
	 */
	function get_template()
	{
		$config = cache_read( 'config', 'sysconfig' );
		$index_temp_sql = "SELECT `id`, `name` FROM `".DB_PRE."template` WHERE `style` = '{$config['site_style']}' AND `type` = 'article_category_index_temp'";
		$index_temp = $this->db->query( $index_temp_sql );
		$list_temp_sql = "SELECT `id`, `name` FROM `".DB_PRE."template` WHERE `style` = '{$config['site_style']}' AND `type` = 'article_list_temp'";
		$list_temp = $this->db->query( $list_temp_sql );
		$article_temp_sql = "SELECT `id`, `name` FROM `".DB_PRE."template` WHERE `style` = '{$config['site_style']}' AND `type` = 'article_temp'";
		$article_temp = $this->db->query( $article_temp_sql );
		return array( 'index_temp' => $index_temp, 'list_temp' => $list_temp, 'article_temp' => $article_temp );
	}
	
	/**
	 * 获取默认模板
	 *
	 * @return array
	 */
	function get_default_temp()
	{
		$config = cache_read( 'config', 'sysconfig' );
		return array( 'index_temp' => $config['ar_cg_index_temp'], 'list_temp' => $config['ar_list_temp'], 'ar_temp' => $config['ar_temp'] );
	}
	
	/**
	 * 获取分类
	 *
	 * @return array
	 */
	function get_column_tree( $ar_no = false )
	{
		$category_tree = cache_read( 'article', 'column' );
		if( !$ar_no )
		{
			return $category_tree;
		}
		$tree = array();
		foreach( $category_tree as $key => $value )
		{
			if( $ar_no )
			{
				$tree[] = array
				(
						'id' => $value['id'],
						'article_no' => $this->db->count( DB_PRE.'article', array( 'cid' => $value['id'], 'draft' => 0, 'recycle' => 0 ) ),
						'draft_no' => $this->db->count( DB_PRE.'article', array( 'cid' => $value['id'], 'draft' => 1 ) ),
						'recycle_no' => $this->db->count( DB_PRE.'article', array( 'cid' => $value['id'], 'recycle' => 1 ) ),
						'name' => $value['name'],
						'pid' => $value['pid'],
						'attribute' => $value['attribute'],
						'jump' => $value['jump'],
						'ifshow' => $value['ifshow'],
						'order' => $value['order']
				);
			}
			else
			{
				$tree[] = array
				(
						'id' => $value['id'],
						'name' => $value['name'],
						'pid' => $value['pid'],
						'attribute' => $value['attribute'],
						'jump' => $value['jump'],
						'ifshow' => $value['ifshow'],
						'order' => $value['order']
				);
			}
		}
		return $tree;
	}
	
	/**
	 * 添加分类到数据库
	 *
	 * @param array $datas
	 * @return bool/int
	 */
	function add_column( $datas )
	{
		if( !empty( $datas['view_group'] ) )
		{
			$view_group = '';
			foreach( $datas['view_group'] as $v )
			{
				$view_group .= $v.' ';
			}
			$datas['view_group'] = trim( $view_group );
		}
		if( !empty( $datas['post_group'] ) )
		{
			$post_group = '';
			foreach( $datas['post_group'] as $v2 )
			{
				$post_group .= $v2.' ';
			}
			$datas['post_group'] = trim( $post_group );
		}
		if( $this->db->insert( DB_PRE.'articlecolumn', $datas ) )
		{
			cache_article_column();
			return $this->db->insert_id();
		}
		return false;
	}
	
	/**
	 * 更新栏目到数据库
	 *
	 * @param int $id
	 * @param array $datas
	 * @return bool
	 */
	function update_column( $id, $datas )
	{
		if( !empty( $datas['view_group'] ) )
		{
			$view_group = '';
			foreach( $datas['view_group'] as $v )
			{
				$view_group .= $v.' ';
			}
			$datas['view_group'] = trim( $view_group );
		}
		if( !empty( $datas['post_group'] ) )
		{
			$post_group = '';
			foreach( $datas['post_group'] as $v2 )
			{
				$post_group .= $v2.' ';
			}
			$datas['post_group'] = trim( $post_group );
		}
		if( $this->db->update( DB_PRE.'articlecolumn', $datas, array( 'id' => $id )) )
		{
			cache_article_column();
			return true;
		}
		return false;
	}
	
	/**
	 * 获取用户组id
	 *
	 * @return array
	 */
	function get_member_group()
	{
		$sql = "SELECT `id`, `name` FROM `".DB_PRE."membergroup` WHERE `type` = 'user'";
		$result = $this->db->query( $sql );
		return $result;
	}
	
	/**
	 * 获取一个分类的详细
	 *
	 * @param unknown_type $id
	 * @return unknown
	 */
	function get_column( $id )
	{
		$result = $this->db->get_where( DB_PRE.'articlecolumn', array( 'id' => $id ) );
		return $result[0];
	}
	
	/**
	 * 检查栏目移动是否合法
	 *
	 * @param int $id
	 * @param int $pid
	 */
	function check_pid( $id, $pid )
	{
		$tree = cache_read( 'article', 'column' );
		$childs = $this->get_child_colum( $id, $tree );
		$childs_arr = array();
		if( !empty( $childs ) )
		{
			foreach( $childs as $v )
			{
				$childs_arr[] = $v['id'];
			}
		}
		if( in_array( $pid, $childs_arr ) )
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 获取子类
	 *
	 * @param int $pid
	 * @param array $tree
	 * @return array
	 */
	function get_child_colum( $pid, $tree )
	{
		$new_cols = array();
		$temp_arr = $tree;
		$record = count( $temp_arr );
		$s = 0;
		$t = 0;
		
		while( true ){
			$find = false;
			foreach( $temp_arr as $key => $val )
			{
				if( $val["pid"] == $pid )
				{
					$new_cols[$s]["pid"] = $pid;
					$new_cols[$s]["id"] = $val["id"];
					
					$s++;
					
					$pid = $val["id"];
					unset( $temp_arr[$key] );
					$find = true;
					break;
				}
			}
			
			if($find == false)
			{
				reset( $temp_arr );
				$t = $s - 1;
				$find2 = false;
				
				while( $t >= 0 ){
					foreach( $temp_arr as $key2 => $val2 )
					{
						if( $val2["pid"] == $new_cols[$t]["pid"] )
						{
							$pid = $val2["pid"];
							$find2 = true;
							break;
						}
					}
					
					if( $find2 == true )
					{
						break;
					}
					$t--;
				}
				if( $find2 == false )
				{
					break;
				}
			}
			if( $s == $record )
			{
				break;
			}
		}
		return $new_cols;
	}
	
	/**
	 * 检查是否有子分类
	 *
	 * @param int $id
	 */
	function check_column( $id )
	{
		$sql = "SELECT `id` FROM `".DB_PRE."articlecolumn` WHERE `pid` = $id";
		$result = $this->db->query( $sql );
		return empty( $result )?true:false;
	}
	
	/**
	 * 移动栏目
	 *
	 * @param int $id
	 * @param int $pid
	 * @return bool
	 */
	function move( $id, $pid )
	{
		if( $this->db->update( DB_PRE.'articlecolumn', array( 'pid' => $pid ), array( 'id' => $id ) ) )
		{
			cache_article_column();
			return true;
		}
		return false;
	}
	
	/**
	 * 删除栏目
	 *
	 * @param int $id
	 */
	function delete( $id )
	{
		$tree = cache_read( 'article', 'column' );
		$childs = $this->get_child_colum( $id, $tree );
		$childs_arr = array();
		$childs_arr[] = $id;
		if( !empty( $childs ) )
		{
			foreach( $childs as $v )
			{
				$childs_arr[] = $v['id'];
			}
		}
		foreach( $childs_arr as $v2 )
		{
			$this->db->delete( DB_PRE.'articlecolumn', array( 'id' => $v2 ) );
			$sql = "SELECT `id`, `thumb_pic` FROM `".DB_PRE."article` WHERE `cid` = '$v2'";
			$result = $this->db->query( $sql );
			if( !empty( $result ) )
			{
				foreach( $result as $v3 )
				{
					$this->db->delete( DB_PRE.'article', array( 'id' => $v3['id'] ) );
					$this->db->delete( DB_PRE.'article_content', array( 'aid' => $v3['id'] ) );
					$this->db->delete( DB_PRE.'tags', array( 'aid' => $v3['id'] ) );
					@unlink( SUNCMS.'uploads/thumb/'.$v3['small_pic'] );
					@unlink( SUNCMS.'datas/cache/article/column_'.$v3['id'].'.php' );
				}
			}
			unset( $sql, $result );
		}
		cache_article_column();
		return $childs_arr;
	}
	
	/**
	 * 更新栏目排序
	 *
	 * @param int $order
	 */
	function update_order( $order )
	{
		$i = 0;
		foreach( $order as $k=> $v )
		{
			$this->db->update( DB_PRE.'articlecolumn', array( 'order' => $v ), array( 'id' => $k ) )?'':$i = 1;
			unset( $order[$k] );
		}
		cache_article_column();
		return $i == 0?true:false;
	}
	
}